<?php

namespace app\admin\controller\api;

use app\common\model;
use think\Log;

/**
 * 用户
 * @internal
 */
class User extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    public function profile()
    {
        $id = $this->request->request('id');
        $result = model\User::where('id', $id)->field('id,username,nickname,gender,mobile,avatar,successions,maxsuccessions,logintime,loginip,status,school_id')->find();

        if (!strstr($result['avatar'], 'http') && strstr($result['avatar'], '/uploads/')) {
            $result['avatar'] = config('mp.url') . $result['avatar'];
        }

        $this->success('', $result);
    }

    public function lists()
    {
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $school_ids = $this->getSchoolIds();

        $total = model\User::where($where)
            ->where('status', 'normal')
            ->whereIn('school_id', $school_ids)
            ->count();

        $list = model\User::where($where)
            ->where('status', 'normal')
            ->whereIn('school_id', $school_ids)
            ->field('id,username,nickname,gender,mobile,avatar,successions,maxsuccessions,logintime,loginip,status')
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        foreach ($list as $key => $value) {
            if (!strstr($value['avatar'], 'http') && strstr($value['avatar'], 'uploads/')) {
                $list[$key]['avatar'] = config('app_url') . $value['avatar'];
            }
        }

        $result = array("total" => $total, "rows" => $list);

        $this->success('', $result);
    }


}